<?php

class MPage extends CI_Model{

	public function __construct()
	    {
	        parent::__construct();
	    }
	
	
	function getPage($id){
	    $data = array();
	    $this->db->where('page_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('page');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
	
	function getPagePath($path){
	    $data = array();
	    $this->db->where('path',db_clean($path));
	    $this->db->where('activate', 1);
	    $this->db->limit(1);
	    $Q = $this->db->get('page');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
		
	 function getAllPages(){
	     $data = array();
	     $this->db->order_by("date_created", "asc");
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	 
	 function getAllActivePages(){
	     $data = array();
	     $this->db->order_by("date_created", "asc");
	     $this->db->where("activate", 1);
	     $this->db->where("parent_id < ", 1);
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	 
	function getActiveSubPages($id){
	     $data = array();
	     $this->db->order_by("date_created", "asc");
	     $this->db->where("activate", 1);
	     $this->db->where('parent_id',id_clean($id));
	     $this->db->where("parent_id > ", 0);
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	 
	 function getMainMenuDropdown(){
	     $data = array();
	     $this->db->order_by("date_created", "asc");
	     $this->db->where("activate", 1);
	     $this->db->where("parent_id < ", 1);
	     $Q = $this->db->get('page');
	     if ($Q->num_rows() > 0){
	       $data[0] = '...Select Parent...';
	       foreach ($Q->result_array() as $row){
	         $data[$row['page_id']] = $row['name'];
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	
	
		
	 function addPage(){
		$data = array( 
			'name' => db_clean($_POST['name']),
			'keyword' => db_clean($_POST['keyword']),
			'description' => db_clean($_POST['description']),
			'path' => db_clean($_POST['path']),
			'content' => $_POST['content'],
			'external_content' => $_POST['external_resource'],
			'parent_id' => db_clean($_POST['parent_id']),
			'activate' => db_clean($_POST['activate'])
		);
		$this->db->set('date_created', 'NOW()', FALSE);
		$this->db->insert('page', $data);	 
	 }
	 
	 function updatePage(){
		$data = array( 
			'name' => db_clean($_POST['name']),
			'keyword' => db_clean($_POST['keyword']),
			'description' => db_clean($_POST['description']),
			'path' => db_clean($_POST['path']),
			'content' => $_POST['content'],
			'external_content' => $_POST['external_resource'],
			'parent_id' => db_clean($_POST['parent_id']),
			'activate' => db_clean($_POST['activate'])
		
		);
	
	 	$this->db->where('page_id', id_clean($_POST['page_id']));
	 	$this->db->set('date_modified', 'NOW()', FALSE);
		$this->db->update('page', $data);	
	 
	 }
	 function inactivePage($id){
	 	$data = array('activate' => 0);
	 	$this->db->where('page_id', id_clean($id));
		$this->db->update('page', $data);	
	 }
	 
	 function deletePage($id){
	 	$this->db->where('page_id', id_clean($id));
		$this->db->delete('page');	
	 }
	 
	
}

?>